package com.java110.mall.goods.cmd.housekeepingServ;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.shop.ShopDto;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.mall.goods.bmo.housekeepingServ.ISaveHousekeepingServBMO;
import com.java110.po.housekeeping.HousekeepingServPo;
import com.java110.po.product.ProductDetailPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import java.util.List;

@Java110Cmd(serviceCode = "housekeepingServ.saveHousekeepingServ")
public class SaveHousekeepingServCmd extends Cmd {

    @Autowired
    private ISaveHousekeepingServBMO saveHousekeepingServBMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "servName", "请求报文中未包含servName");
        Assert.hasKeyAndValue(reqJson, "hktId", "请求报文中未包含hktId");
        Assert.hasKeyAndValue(reqJson, "keyword", "请求报文中未包含keyword");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含state");
        Assert.hasKeyAndValue(reqJson, "repairWay", "请求报文中未包含repairWay");
        Assert.hasKeyAndValue(reqJson, "returnVisitFlag", "请求报文中未包含returnVisitFlag");
        Assert.hasKeyAndValue(reqJson, "coverPhoto", "请求报文中未包含商品封面");
        Assert.hasKeyAndValue(reqJson, "context", "请求报文中未包含商品描述");
        Assert.hasKey(reqJson, "carouselFigurePhoto", "请求报文中未包含轮播图");
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(reqJson.getString("shopId"));
        shopDto.setState(ShopDto.STATE_Y);//审核通过的店铺
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if (ListUtil.isNull(shopDtos)) {
            throw new CmdException("审核通过店铺才可以添加");
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        String coverPhoto = reqJson.getString("coverPhoto");
        //轮播图
        JSONArray carouselFigurePhoto = reqJson.getJSONArray("carouselFigurePhoto");
        if (ListUtil.isNull(carouselFigurePhoto)) {
            throw new IllegalArgumentException("请求报文中未包含轮播图");
        }
        //服务信息
        HousekeepingServPo housekeepingServPo = BeanConvertUtil.covertBean(reqJson, HousekeepingServPo.class);
        //服务详情
        ProductDetailPo productDetailPo = new ProductDetailPo();
        productDetailPo.setShopId(reqJson.getString("shopId"));
        productDetailPo.setContent(reqJson.getString("context"));
        //关键字
        String keywords = reqJson.getString("keyword").trim();

        ResponseEntity<String> response = saveHousekeepingServBMOImpl.save(coverPhoto, carouselFigurePhoto, productDetailPo, housekeepingServPo, keywords);
        context.setResponseEntity(response);
    }
}
